package com.example.myapplication.ui.components

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.SwapHoriz
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.example.myapplication.data.model.StorageItem
import com.example.myapplication.viewmodel.StorageDetailViewModel
import com.example.myapplication.ui.components.AddItemDialog
import com.example.myapplication.ui.components.MoveItemDialog

@Composable
fun ItemActionHandler(
    selectedItem: StorageItem?,
    showItemActionSheet: Boolean,
    onDismissActionSheet: () -> Unit,
    viewModel: StorageDetailViewModel
) {
    var showEditItemDialog by remember { mutableStateOf(false) }
    var showMoveItemDialog by remember { mutableStateOf(false) }

    if (showItemActionSheet && selectedItem != null) {
        ActionBottomSheet(
            onDismiss = onDismissActionSheet,
            title = selectedItem.name,
            actions = listOf(
                ActionItem(
                    title = "修改",
                    icon = Icons.Default.Edit,
                    onClick = {
                        selectedItem?.let { item ->
                            viewModel.onEditItemInitiated(item)
                        }
                        onDismissActionSheet()
                        showEditItemDialog = true
                    }
                ),
                ActionItem(
                    title = "迁移",
                    icon = Icons.Default.SwapHoriz,
                    onClick = {
                        selectedItem?.let { item ->
                            viewModel.onMoveItemInitiated(item)
                        }
                        onDismissActionSheet()
                        showMoveItemDialog = true
                    }
                ),
                ActionItem(
                    title = "删除",
                    icon = Icons.Default.Delete,
                    onClick = {
                        selectedItem?.let { item ->
                            viewModel.onDeleteItemInitiated(item)
                        }
                        onDismissActionSheet()
                    },
                    isDestructive = true
                )
            )
        )
    }

    if (showEditItemDialog && viewModel.uiState.value.itemToEdit != null) {
        AddItemDialog(
            onDismiss = {
                viewModel.onEditItemCancelled()
                showEditItemDialog = false
            },
            onConfirm = { name, description, quantity, imagePath ->
                viewModel.uiState.value.itemToEdit?.let {
                    viewModel.onEditItemConfirmed(name, description, quantity, imagePath)
                }
                showEditItemDialog = false
            },
            isEditMode = true,
            initialName = viewModel.uiState.value.itemToEdit?.name ?: "",
            initialDescription = viewModel.uiState.value.itemToEdit?.description ?: "",
            initialQuantity = viewModel.uiState.value.itemToEdit?.quantity ?: 1,
            initialImagePath = viewModel.uiState.value.itemToEdit?.imagePath
        )
    }

    if (showMoveItemDialog && viewModel.uiState.value.itemToMove != null) {
        MoveItemDialog(
            itemToMove = viewModel.uiState.value.itemToMove!!,
            allSpaces = viewModel.uiState.value.allSpaces,
            onDismiss = {
                viewModel.onMoveItemCancelled()
                showMoveItemDialog = false
            },
            onConfirm = { newSpaceId ->
                viewModel.onMoveItemConfirmed(newSpaceId)
                showMoveItemDialog = false
            }
        )
    }
}
